﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class DonGiaDAL
    {
        PhongMachTuDBDataContext db;
        public IQueryable LayDSDonGia(string maThuoc)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.DONGIAs
                    where s.MaThuoc == maThuoc
                    select new
                    {
                       s.MaDonGia,
                       s.DONVI.TenDonVi,
                       s.DonGia1,
                       s.THUOC.TenThuoc
                    };
            return q;
        }
        public int LayDSDonGia(string maThuoc,string maDonVi)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.DONGIAs
                    where s.MaThuoc == maThuoc && s.MaDonVi==maDonVi
                    select s;
            int gia = 0;
            foreach (var item in q)
            {
                gia = item.DonGia1;
            }
            return gia;
        }
        public bool ThemDonGia(DONGIA s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                DONGIA r = new DONGIA();
                r.MaDonVi = s.MaDonVi;
                r.MaThuoc = s.MaThuoc;
                r.DonGia1 = s.DonGia1;

                db.DONGIAs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaDonGia(DONGIA s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                DONGIA r = db.DONGIAs.Single(id => id.MaDonGia == s.MaDonGia);
                r.MaDonVi = s.MaDonVi;
                r.MaThuoc = s.MaThuoc;
                r.DonGia1 = s.DonGia1;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaDonGia(int MaDonGia)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.DONGIAs
                             where d.MaDonGia == MaDonGia
                             select d;
                if (delete.Count() > 0)
                {
                    db.DONGIAs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
